package com.example.smartcity_poverty.ui.AllService.VolunteerService.ui.homeOne

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.App.Companion.url
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.ServiceBean.VolunteerService.VtBannerModel
import com.example.smartcity_poverty.logic.ServiceBean.VolunteerService.VtHomeNewsModel
import com.example.smartcity_poverty.logic.util.Util.glide
import com.example.smartcity_poverty.logic.util.Util.show
import com.example.smartcity_poverty.ui.AllService.VolunteerService.adapter.VtHomeOneNewsAdapter
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator
import kotlinx.android.synthetic.main.fragment_vt_home_one.*

/**
 * 志愿服务主页
 */
class VtHomeOneFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_vt_home_one, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        runThread()
    }

    private fun runThread() = coroutine {
        // News
        getHomeNews()


        // Banner
        getBanner()


    }


    private suspend fun getBanner() {
        if (getToken().isNotEmpty()) {
            api.getVtBanner(getToken()).apply {
                if (this.code == 200) {
                    val rows = this.data
                    activity?.runOnUiThread {
                        vtHomeOneBanner.apply {
                            this.adapter = object :
                                BannerImageAdapter<VtBannerModel.DataBean>(ArrayList<VtBannerModel.DataBean>().apply {
                                    this.addAll(rows!!)
                                }) {
                                override fun onBindView(
                                    p0: BannerImageHolder?,
                                    p1: VtBannerModel.DataBean?,
                                    p2: Int,
                                    p3: Int
                                ) {
                                    glide(url + p1?.imgUrl, p0?.imageView!!)
                                }
                            }
                            this.addBannerLifecycleObserver(this@VtHomeOneFragment).indicator =
                                CircleIndicator(context)
                            this.setBannerRound(25f)
                        }
                    }
                } else activity?.runOnUiThread { this.msg?.show() }
            }
        } else activity?.runOnUiThread { "请登录！".show() }

    }


    private val arrayNews = ArrayList<VtHomeNewsModel.RowsBean>()
    private suspend fun getHomeNews() {
        if (getToken().isNotEmpty()) {
            arrayNews.clear()
            api.getVtHomeNews(getToken()).apply {
                if (this.code == 200) {
                    arrayNews.addAll(this.rows!!)

                    activity?.runOnUiThread {
                        vtHomeNewsRv.apply {
                            this.isNestedScrollingEnabled = false
                            this.layoutManager = LinearLayoutManager(context)
                            this.adapter =
                                VtHomeOneNewsAdapter(arrayNews, R.layout.re_vt_home_one_news)
                            this.adapter?.notifyItemChanged(arrayNews.size)
                        }
                    }
                } else activity?.runOnUiThread { this.msg?.show() }
            }
        } else activity?.runOnUiThread { "请登录".show() }

    }


}